package com.itic.appbase.framework.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


public class IpAddressHelper {
	
	/**
     * 获取用户id测试方法
     * @param request
     * @return
     */
    public static String getRemortIP(HttpServletRequest request) {

        if (request.getHeader("x-forwarded-for") == null) {

            return request.getRemoteAddr();    

        }     

        return request.getHeader("x-forwarded-for");     

    }
    
    public static String getRemortIP4(){
    	String ip = null;
    	@SuppressWarnings("rawtypes")
		Enumeration allNetInterfaces;
    	try {
	    	allNetInterfaces = NetworkInterface.getNetworkInterfaces();
	    	while (allNetInterfaces.hasMoreElements()) {
		    	NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
		    	List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
		    	for (InterfaceAddress add : InterfaceAddress) {
		    	InetAddress Ip = add.getAddress();
		    	if (Ip != null && Ip instanceof Inet4Address) {
		    	ip = Ip.getHostAddress();
	    	}
    	}
    	}
    	} catch (SocketException e) {
    	// TODO Auto-generated catch block
    	// logger.warn("获取本机Ip失败:异常信息:"+e.getMessage());
    	}
    	return ip;
    }
    
    
    
}
